#include "Run.h"
#include "Meld.h"
#include "Card.h"
#include "Set.cpp"
#include "SetIter.cpp"
#include <iostream>
#include <string>

using std::ostream;
using std::cout;
using std::endl;
using std::string;

Run:: Run(){
}
Run:: Run(Set<Card> aSet){
  cardSet = Set<Card>(aSet);
}

Run:: Run(const Run& copyRun){
  cardSet = Set<Card>(copyRun.getCardSet());
}

Run:: Run(const Meld& copyMeld){
  cout<<"Creates meld"<<endl;
  cardSet = Set<Card>(copyMeld.getCardSet());
}

Run:: ~Run(){
}

Set<Card> Run::getValidCards(){
  //  cout<<"ENDL"<<endl;

  Set<Card> thisSet;
  SetIter<Card> thisIter(cardSet);
  int numAces = 0;
  int numReqAces = 0;

  Card thisCard;
  do{
    if(thisIter.hasNext()){
      thisCard = thisIter.next();
      if(thisCard.face!=1){
	if(thisCard.face==2){
	  numReqAces++;
	  if(numAces<numReqAces)
	    thisSet.add(Card(thisCard.face-1,thisCard.suit));
	}
	else
	thisSet.add(Card(thisCard.face-1,thisCard.suit));
      }
      else
	numAces++;
    }
  }
  while(thisIter.hasNext()&&thisCard.face==1);
  while(thisIter.hasNext()){
    thisCard = thisIter.next();
    if(thisCard.face==1)
      numAces++;
  }
  if(thisCard.face!=1){
    //    cout<<"Last: "<<thisCard<<endl;
    if(thisCard.face==13){
      numReqAces++;
      if(numAces<numReqAces)
	thisSet.add(Card((thisCard.face%13)+1,thisCard.suit));
    }
    else
      thisSet.add(Card((thisCard.face%13)+1,thisCard.suit));
  }

  //  cout<<"VALID SET OF CARDS: "<<thisSet<<endl;

  return thisSet;
}

//Checkers
bool Run::isValid() const{
  return validify()==1;
}
